package com.baizw.cultivation.module.admin.controller;

import cn.hutool.core.collection.CollUtil;
import com.baizw.cultivation.common.response.ResultBean;
import com.baizw.cultivation.common.response.ResultPage;
import com.baizw.cultivation.module.admin.entity.DictDataEntity;
import com.baizw.cultivation.module.admin.page.DictDataSearch;
import com.baizw.cultivation.module.admin.service.DictDataService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 字典数据管理
 *
 * @author 剧终1204
 * @version V1.0
 * @date 2020年07月22日 12:39
 */
@RestController
@Api(tags = "字典数据管理")
@RequestMapping("/api/dict/data")
public class DictDataController {

    @Autowired
    private DictDataService dataService;

    @PostMapping("/page")
    @ApiOperation("分页查询字典数据")
    public ResultPage dataListPage(@RequestBody DictDataSearch search) {
        IPage<DictDataEntity> dataSearch = dataService.pageByParams(search);
        return ResultPage.success(dataSearch);
    }

    @ApiOperation("根据字典类型查询字典数据")
    @GetMapping("/list/{dictType}")
    public ResultBean dataList(@PathVariable String dictType) {
        return ResultBean.ok(dataService.listByType(dictType));
    }

    @ApiOperation("获取字典数据详情")
    @GetMapping("/detail/{id}")
    public ResultBean dictDataDetail(@PathVariable Long id) {
        DictDataEntity dataEntity = dataService.getById(id);
        return ResultBean.ok(dataEntity);
    }

    @ApiOperation("新增字典数据")
    @PostMapping("/insert")
    @CacheEvict(cacheNames = "sys_dict", allEntries = true)
    public ResultBean add(@Validated @RequestBody DictDataEntity dict) {
        return ResultBean.ok(dataService.save(dict));
    }

    @ApiOperation("修改字典数据")
    @PutMapping("/update")
    public ResultBean edit(@Validated @RequestBody DictDataEntity dict) {
        return ResultBean.ok(dataService.updateById(dict));
    }

    @ApiOperation("删除字典数据")
    @DeleteMapping("/remove/{dictIds}")
    public ResultBean remove(@PathVariable Long[] dictIds) {
        return ResultBean.ok(dataService.removeByIds(CollUtil.toList(dictIds)));
    }
}
